/*******************************************************************************
 * $Header: /cvsroot/oloa/org.gocom.bpm.platform.model/src/org/gocom/bpm/platform/model/web/taglib/GraphicsImpl.java,v 1.1 2011/07/03 02:30:16 hufulai Exp $
 * $Revision: 1.1 $
 * $Date: 2011/07/03 02:30:16 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved.
 * 
 * Created on 2010-5-26
 *******************************************************************************/

package com.atozsoft.wf.taglib;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.JspException;

import com.primeton.workflow.model.definition.Activity;
import com.primeton.workflow.model.definition.Transition;
import com.primeton.workflow.model.definition.WFNote;
import com.primeton.workflow.model.definition.WFProcess;


public class GraphicsImpl implements IGraphics{
    private Object id;
    private WFProcess process;
    private List<Connection> connections;
    private HashMap<String,State> states;
    
    private ScriptGenerator scriptGenerator;
    private Iterator iterator;
    private State nextState;
    private ProcessGraphTag processGraphTag;
    
    public GraphicsImpl(Object id, WFProcess process, ScriptGenerator scriptGenerator, ProcessGraphTag processGraphTag)
        throws JspException
    {
        connections = new ArrayList<Connection>();
        states = new HashMap<String,State>();
        setId(id);
        setScriptGenerator(scriptGenerator);
        setWFProcess(process);
        this.processGraphTag = processGraphTag;
        buildProcessDefGraph(process.getTransitions().iterator());
        iterator = states.values().iterator();
    }

    private void buildProcessDefGraph(Iterator transitions)throws JspException
    {
    	while(transitions.hasNext())
        {
            Transition transition = (Transition)transitions.next();
            Activity fromActivity = transition.getProcess().findActivity(transition.getFrom());
            Activity toActivity = transition.getProcess().findActivity(transition.getTo());
            if(fromActivity != null && toActivity != null)
            {
                Connection connection = new ConnectionImpl(transition);
                connection.getFromState().getExtendAttribute().put("processDefId", getId());
                connection.getToState().getExtendAttribute().put("processDefId", getId());
                processGraphTag.assembleMouseEvents(processGraphTag, connection.getFromState());
                processGraphTag.assembleActivityTitle(connection.getFromState());
                processGraphTag.assembleActivityIcon(connection.getFromState());
                processGraphTag.assembleMouseEvents(processGraphTag, connection.getToState());
                processGraphTag.assembleActivityTitle(connection.getToState());
                processGraphTag.assembleActivityIcon(connection.getToState());
                connections.add(connection);
                states.put(connection.getFromState().getID(), connection.getFromState());
                states.put(connection.getToState().getID(), connection.getToState());
            }
        }
    }

    public List<Connection> getConnections()
    {
        return connections;
    }

    public void setConnections(List<Connection> connections)
    {
        this.connections = connections;
    }

    public HashMap<String,State> getStates()
    {
        return states;
    }

    public void setStates(HashMap<String,State> states)
    {
        this.states = states;
    }

    public ScriptGenerator getScriptGenerator()
    {
        return scriptGenerator;
    }

    public void setScriptGenerator(ScriptGenerator scriptGenerator)
    {
        this.scriptGenerator = scriptGenerator;
    }

    public WFProcess getWFProcess()
    {
        return process;
    }

    public void setWFProcess(WFProcess processDefine)
    {
        this.process = processDefine;
    }

    public Object getId()
    {
        return id;
    }

    public void setId(Object id)
    {
        this.id = id;
    }

    public void drawConnections(StringBuffer sbLine, StringBuffer sbLineText)
    {
        scriptGenerator.handleConnectors(sbLine, sbLineText, getConnections());
    }

    public void drawStates(StringBuffer sbState)
    {
        for(Iterator stateIterator = states.values().iterator(); 
        stateIterator.hasNext(); 
        scriptGenerator.handleState(sbState, (State)stateIterator.next()));
    }
    
    /**
     * ��ʾע��
     * @param sbNotes
     */
    public void drawNotes(StringBuffer sbNotes){
        for(Iterator noteIterator = process.getNotes().iterator(); 
        noteIterator.hasNext(); 
        scriptGenerator.handleNote(sbNotes, (WFNote)noteIterator.next()));
    }
    
    public void drawHeader(StringBuffer buf){
    	scriptGenerator.handleHeader(buf, process.getName(), processGraphTag.getProcessInstName());
    }

    public boolean hasNextState()
    {
        if(iterator != null && iterator.hasNext()){
            nextState = (State)iterator.next();
            return true;
        } else{
            return false;
        }
    }

    public State getNextState()
    {
        return nextState;
    }


}
